package cn.iocoder.yudao.module.erp.dal.mysql.gi;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.erp.controller.admin.gi.gidn.vo.GiDnPageReqVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.gi.GiDnDO;
import org.apache.ibatis.annotations.Mapper;

/**
 * ERP物流义乌仓库 Mapper
 * @author 天使管理员
 */
@Mapper
public interface GiDnMapper extends BaseMapperX<GiDnDO> {

    default PageResult<GiDnDO> selectPage(GiDnPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<GiDnDO>()
                .likeIfPresent(GiDnDO::getNo, reqVO.getNo())
                .eqIfPresent(GiDnDO::getStoreType, reqVO.getStoreType())
                .eqIfPresent(GiDnDO::getInCount, reqVO.getInCount())
                .likeIfPresent(GiDnDO::getInName, reqVO.getInName())
                .eqIfPresent(GiDnDO::getInVolume, reqVO.getInVolume())
                .eqIfPresent(GiDnDO::getWeight, reqVO.getWeight())
                .eqIfPresent(GiDnDO::getDensity, reqVO.getDensity())
                .eqIfPresent(GiDnDO::getDeliveryPhone, reqVO.getDeliveryPhone())
                .likeIfPresent(GiDnDO::getDeliveryName, reqVO.getDeliveryName())
                .eqIfPresent(GiDnDO::getEarpayAmount, reqVO.getEarpayAmount())
                .eqIfPresent(GiDnDO::getInPic, reqVO.getInPic())
                .eqIfPresent(GiDnDO::getLine, reqVO.getLine())
                .eqIfPresent(GiDnDO::getFile, reqVO.getFile())
                .eqIfPresent(GiDnDO::getNote, reqVO.getNote())
                .eqIfPresent(GiDnDO::getStatus, reqVO.getStatus())
                .betweenIfPresent(GiDnDO::getCreateTime, reqVO.getCreateTime())
                .eqIfPresent(GiDnDO::getDeleted,0)
                .orderByDesc(GiDnDO::getId));
    }

    // 获取当前最大 ID
    default String getCurrentMaxOrderNumber() {
        GiDnDO result = selectOne(new LambdaQueryWrapperX<GiDnDO>()
                .select(GiDnDO::getId)
                .orderByDesc(GiDnDO::getId)
                .last("LIMIT 1"));
        // 判断查询结果是否为null
        if (result != null) {
            return result.getId();  // 如果查询到数据，返回ID
        } else {
            return "0001";  // 如果数据库为空，返回一个默认值，如 "0001
        }
    }
}